<?php
namespace app\index\model;
use app\index\validate\User;
class UserTable extends Common
{
	protected $table = 'user_table';
	protected $auto = ['ip'];
	   protected $insert = ['status' => 1];
	   protected $update = [];  
	   protected function setIpAttr()
	   {
	       return request()->ip();
	   }
	   
	//密码加密
	public function setPasswordAttr($val){
		return md5('hch'.sha1($val).'hch');
	}
	
	
	public function registerData($data){
		/* halt($data); */
		$validate = new User();
		if(!$validate->scene('register')->check($data)){
			return $this->message('400',$validate->getError());
		}
		$res = UserTable::create($data);
		if($res){
			return $this->message('200','注册成功！');
		}else{
			return $this->message('400','注册失败！');
		}
	}
	
	public function loginData($data){
		$validate = new User();
		if(!$validate->scene('login')->check($data)){
			return $this->message('400',$validate->getError());
		}
		$res = UserTable::where('account_number',$data['account_number'])->find();
		if(!$res){
			return $this->message('400','用户不存在！');
		}
		if($res['password'] !== md5('hch'.sha1($data['password']).'hch')){
			return $this->message('400','密码错误！');
		}
		if($res['status'] !== 1){
			return $this->message('400','账号已被禁止登录，请联系管理员。');
		}
		Cookie('username',$res['username']);
		Cookie('id',$res['id']);
		return $this->message('200','登录成功');
	}
	
}